<script setup lang="ts">
import { LeftOutlined, RightOutlined } from '@ant-design/icons-vue';
import {computed, onMounted, reactive, ref} from "vue";
import {getArticleListByCategory} from "@/api/articleController";
import {formatDate} from "@/utils/timeUtils"
import {message} from "ant-design-vue";

// 定义数据
const dataList = ref<API.ArticlesVO[]>([])
const total = ref(0)
const loading = ref(true)

const categoryName = '汽车'
const searchParams = reactive<API.ArticlesQueryRequest>({
  current: 1,
  pageSize: 20,
})
// 分页参数
const pagination = computed(() => {
  return {
    current: searchParams.current,
    pageSize: searchParams.pageSize,
    total: total.value,
    onChange: (page: number, pageSize: number) => {
      searchParams.current = page
      searchParams.pageSize = pageSize
      console.log(searchParams.current)
      fetchData()
    },
  }
})


// 获取数据
const fetchData = async () => {
  const res = await getArticleListByCategory({
    categoryName,
    ...searchParams,
  })
  if (res.data.data) {
    dataList.value = res.data.data ?? []
    total.value = res.data.data.length ?? 0
  } else {
    message.error('获取数据失败，' + res.data.message)
  }
}
// 页面加载时请求一次
onMounted(() => {fetchData()})

</script>

<template>
  <div id="auto_home">
    <div class="content">
      <div class="container">
        <a-row>
          <a-col :span="24" :lg="16">
            <div class="article_side_left">
              <div class="carousel">
                <a-carousel arrows autoplay>
                  <template #prevArrow>
                    <div class="custom-slick-arrow" style="left: 10px; z-index: 1">
                      <LeftOutlined style="color: black; background: #f7f7f7"/>
                    </div>
                  </template>
                  <template #nextArrow>
                    <div class="custom-slick-arrow" style="right: 10px">
                      <RightOutlined style="color: black; background: #f7f7f7"/>
                    </div>
                  </template>
                  <div  v-for="(item) in dataList.slice(0, 4)">
                    <div class="carousel_content">
                      <div class="carousel_content_info">
                        <div class="realtime_article_text">
                          实时精选
                        </div>
                        <div class="realtime_article_content">
                          <h3>{{ item.title }}</h3>
                          <span>{{ item.author }}</span>
                          <span>{{ formatDate(item.published_at) }}</span>
                        </div>
                      </div>
                      <div class="carousel_content_img">
                        <img class="article_image" :src="item.cover || '/src/assets/img/default.jpg'" alt="cover">
                      </div>
                    </div>
                  </div>
                </a-carousel>
              </div>
              <a-list item-layout="vertical" size="small" :pagination="pagination"
                      :data-source="dataList.slice(10)">
                <template #renderItem="{ item }">
                  <a-list-item key="item.title">
                    <template #actions>
                      <span>{{ item.author }}</span>
                      <span>{{ formatDate(item.published_at) }}</span>
                    </template>
                    <template #extra>
                      <img
                        width="150"
                        alt="cover"
                        :src="item.cover"
                      />
                    </template>
                    <a-list-item-meta class="content_mete" :description="item.summary" :title="item.title">
                    </a-list-item-meta>
                  </a-list-item>
                </template>
              </a-list>
            </div>
          </a-col>

          <a-col :span="0" :md="8">
            <div class="article_side_right">
              <!-- todo: 论坛推荐部分之后按1小时浏览量排序前十，要每1小时才刷新一次 -->
              <h3>论坛推荐</h3>
              <a-list item-layout="horizontal" :data-source="dataList.slice(4, 14)">
                <template #renderItem="{ item, index }">
                  <a-list-item style="padding-inline: 0">
                    <a-list-item-meta>
                      <template #title>
                        <span :style="index < 3 ? { color: 'red' } : {color: '#999999'}" class="ordinal">{{ index + 1 }}</span>
                        <a :href="`/auto/${item.id}`" target="_blank">{{ item.title }}</a>
                      </template>
                    </a-list-item-meta>
                  </a-list-item>
                </template>
              </a-list>
            </div>
          </a-col>
        </a-row>
      </div>
    </div>
  </div>
</template>

<style scoped>
#auto_home {
  background-color: #fcfcfc;

  .content {
    padding-block: 32px;
    display: flex;
    justify-content: center;
  }

  .container {
    width: 85%;
  }

  @media (max-width: 768px) {
    .container {
      width: 100%;
    }
  }

  .article_side_left, .article_side_right {
    margin: 36px 12px;
  }

  .article_side_right {
    height: 92%;
    border-left: darkgray solid 1px;
    padding-left: 24px;
  }

  .content_mete :deep(.ant-list-item-meta-description) {
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  :deep(.ant-list-item-meta-title) {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .ordinal {
    margin-right: 16px;
  }

  .carousel {
    margin-bottom: 16px;
  }

  :deep(.slick-slide) {
    text-align: center;
    height: 300px;
    background: #f0f0f0;
    overflow: hidden;
    border-radius: 10px;
  }
  :deep(.slick-arrow .anticon) {
    background: transparent !important;
  }
  :deep(.slick-arrow.custom-slick-arrow) {
    background-color: transparent !important;
  }
  :deep(.slick-arrow.custom-slick-arrow) {
    width: 25px;
    height: 25px;
    font-size: 25px;
    color: #fff;
    background-color: rgba(31, 45, 61, 0.11);
    transition: ease all 0.3s;
    opacity: 0.6;
    z-index: 1;
  }
  :deep(.slick-arrow.custom-slick-arrow:before) {
    display: none;
  }
  :deep(.slick-arrow.custom-slick-arrow:hover) {
    color: #fff;
    opacity: 0.8;
  }
  :deep(.slick-dots) {
    justify-content: left;
    margin-left: 24px;
  }
  :deep(.slick-dots li) {
    justify-content: center;
    margin-inline: 0;
    width: 20px;
  }
  :deep(.slick-dots button) {
    border-radius: 5px;
    background: #c1c1c1;
    width: 4px;
    height: 4px;
    margin-left: 12px;
    margin-right: 4px;
  }
  :deep(.slick-dots .slick-active button) {
    background: black;
    width: 20px;
    margin-left: 4px;
  }
  :deep(.ant-list .ant-list-pagination) {
    display: flex;
    justify-content: center;
  }
  :deep(.ant-list .ant-pagination-options) {
    display: none;
  }

  :deep(.ant-carousel .slick-list .slick-slide >div>div) {
    display: block !important;
  }

  .carousel_content {
    display: flex;
    justify-content: space-between;
    height: 300px;
    margin-left: 36px;
    margin-right: 1px;
  }

  .carousel_content_info {
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    align-items: flex-start;
    max-width: 40%;
    height: 300px;
    padding: 4px;
    padding-top: 40px;
  }

  .realtime_article_text {
    background: #c6301f;
    color: white;
    font-weight: bold;
    font-size: 12px;
    border-radius: 4px;
    width: 64px;
    height: 24px;
    display: flex;
    justify-content: center;
    align-items: center;
    text-align: center;
  }

  .realtime_article_content {
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    margin-top: 28px;

    h3 {
      font-size: 26px;
      font-weight: bolder;
      line-height: 1.5;
      text-align: left;
      display: -webkit-box;
      -webkit-line-clamp: 3;
      -webkit-box-orient: vertical;
      overflow: hidden;
      margin-right: 12px;
    }

    span {
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: smaller;
      color: #aaaaaa;
      height: 24px;
    }
  }

  .carousel_content_img {
    position: relative;
    display: flex;
    justify-content: center;
    width: 510px;
    height: 300px;
    overflow: hidden;
  }

  .article_image {
    position: static;
    transform: none;
    height: 100%;
    min-width: 200px;
    object-fit: cover;
    flex-shrink: 0;
  }

}


</style>
